package com.st.controller.background;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.st.entity.Order;
import com.st.entity.OrderGoods;
import com.st.entity.User;
import com.st.service.IOrderService;
import com.st.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IUserService userService;
    //订单列表页
    @RequestMapping("query")
    public ModelAndView query(Order order){

        ModelAndView mv =new ModelAndView();
        //判断页码变量是否为空，如果为空就赋值1 表示第一页
        if (order.getPageNum() == null) {
            order.setPageNum(1);
        }
        //一个查5条
        order.setPageSize(5);
        //分页查询，注意该方法会对后面的第一个查询做分页
        PageHelper.startPage(order.getPageNum(), order.getPageSize());
        //1.查询商品列表（分页查询+条件查询）
        List<Order> list = orderService.query(order);
        //把查出来的列表放到pageInfo中
        //分页信息，pageNum-页码 pageSize-单页记录数 ，pages-总页数
        PageInfo pageInfo = new PageInfo(list);
        mv.addObject("pageInfo", pageInfo);
        mv.addObject("order",order);
        mv.setViewName("/background/order");
        return mv;
    }
    //订单详情
    @RequestMapping("detail")
    public ModelAndView list(String orderNo){
        ModelAndView mv =new ModelAndView();
        //1查询订单信息 查到的订单信息
        List<Order> list = orderService.query(new Order(orderNo));
        Order order=list.get(0);
        //2查询订单商品详情
        List<OrderGoods> list2=orderService.list(orderNo);
        mv.addObject("order",order);
        mv.addObject("list2",list2);
        mv.setViewName("/background/order_goods");
        return mv;
    }
    //后台商家同意退款
    @RequestMapping("shipments")
    public String shipments(String ids) {

       Integer aa= orderService.goodstuikuanchenggong(ids);
       if (aa==1){
           Order order=new Order(ids);
           List<Order> list = orderService.query(order);
           BigDecimal bb=list.get(0).getOrderMoney();
           Integer cc= list.get(0).getUserId();
           User user=new User();
           user.setId(cc);
           user.setUserMoney(bb);
           userService.updateMoney1(user);
       }
        //跳转到列表页
        return "redirect:query";
    }
}
